package com.wuliu.framework.config;

import com.wuliu.common.constant.RabbitMQConstants;
import org.springframework.amqp.core.*;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Bean;

/**
 * @author realue
 * @version 1.0
 * @date 2024-04-24 10:46
 */
@Configuration
public class RabbitMQConfig {

    /**
     * 定义队列
     *
     * @return
     */
    @Bean
    public Queue smsQueue() {
        // durable:是否持久化，true表示持久化，false表示不持久化
        return new Queue(RabbitMQConstants.SMS_QUEUE_NAME, true);
    }

    /**
     * 定义Fanout交换机
     *
     * @return
     */
    @Bean
    public Exchange smsExchange() {
        return new FanoutExchange(RabbitMQConstants.SMS_EXCHANGE_NAME);
    }

    /**
     * 绑定队列和交换机
     *
     * @return
     */
    @Bean
    public Binding binding() {
        return BindingBuilder.bind(smsQueue()).to(smsExchange()).with("").noargs();
    }

    /**
     * 定义订单延迟队列
     *
     * @return
     */
    @Bean
    public Queue orderDelayQueue() {
        return new Queue(RabbitMQConstants.ORDER_DELAY_QUEUE_NAME, true);
    }

    /**
     * 定义订单延迟交换机
     *
     * @return
     */
    @Bean
    public Exchange orderDelayExchange() {
        return ExchangeBuilder.fanoutExchange(RabbitMQConstants.ORDER_DELAY_EXCHANGE_NAME)
                .delayed().durable(true).build();
    }

    /**
     * 绑定订单延迟队列和交换机
     *
     * @return
     */
    @Bean
    public Binding orderBinding() {
        return BindingBuilder.bind(orderDelayQueue()).to(orderDelayExchange()).with("").noargs();
    }

    /**
     * 定义消息转换器
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}
